﻿using Infrastructure.UI;
using IRRemoteControl;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Resources;
using Windows.Devices.Sensors;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Networking.Proximity;
using Windows.Storage;
using Windows.System;
using Windows.System.Threading;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

namespace RCHelicopter
{
    public sealed partial class MainPage : RCHelicopter.Common.LayoutAwarePage
    {
        ResourceLoader loader = new ResourceLoader();

        TranslateTransform leftStickTransform = new TranslateTransform();
        TranslateTransform rightStickTransform = new TranslateTransform();

        BinaryIREncoder ir = new BinaryIREncoder(38200, new double[] { 0.00204, 0.002, 0.00038 }, new double[] { 0.00022, 0.00038 }, new double[] { 0.0006, 0.00038 });
        bool isOn;
        DispatcherTimer cmdTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };

        public MainPage()
        {
            this.EnablePrivacyPolicy(PrivacyPolicy.RunXiaDianQi);
            this.InitializeComponent();
        }

        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            leftStick.ManipulationMode = ManipulationModes.TranslateY | ManipulationModes.TranslateInertia;
            leftStick.RenderTransform = leftStickTransform;

            rightStick.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY | ManipulationModes.TranslateInertia;
            rightStick.RenderTransform = rightStickTransform;

            cmdTimer.Tick += cmdTimer_Tick;
            cmdTimer.Start();

        }

        void cmdTimer_Tick(object sender, object e)
        {
            if (isOn && leftStickTransform.Y < -10)
            {
                var throttle = (byte)((leftStickTransform.Y - 0) / (-200 - 0) * (255 - 128) + 128); // 0 ~ 255  139
                var leftRight = (byte)((100 - rightStickTransform.X) / (100 - -100) * (0 - 128) + 128); // -64 ~ 63
                var fwdBack = (byte)((-100 - rightStickTransform.Y) / (-100 - 100) * (0 - 128) + 128); // -128 ~ 127

                Debug.WriteLine("{0} {1} {2}", leftRight, fwdBack, throttle);
                ir.SendCommandBytes(leftRight, fwdBack, throttle, 52);
            }
        }

        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }

        private void leftStick_Pressed(object sender, PointerRoutedEventArgs e)
        {

        }

        private void leftStick_ManipulationInertiaStarting(object sender, ManipulationInertiaStartingRoutedEventArgs e)
        {

        }

        private void leftStick_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            leftStickTransform.Y += e.Delta.Translation.Y;

            if (leftStickTransform.Y < -200)
                leftStickTransform.Y = -200;

            if (leftStickTransform.Y > 0)
                leftStickTransform.Y = 0;
        }

        private void rightStick_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            rightStickTransform.X += e.Delta.Translation.X;

            if (rightStickTransform.X > 100)
                rightStickTransform.X = 100;

            if (rightStickTransform.X < -100)
                rightStickTransform.X = -100;

            rightStickTransform.Y += e.Delta.Translation.Y;

            if (rightStickTransform.Y > 100)
                rightStickTransform.Y = 100;

            if (rightStickTransform.Y < -100)
                rightStickTransform.Y = -100;
        }

        private async void buyButton_Click(object sender, RoutedEventArgs e)
        {
            await Launcher.LaunchUriAsync(new Uri("http://item.taobao.com/item.htm?spm=0.0.0.31.DFSvYx&id=17044046838"));
        }

        private void lightToggleButton_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void rightStick_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
            rightStickTransform.X = 0;
            rightStickTransform.Y = 0;
        }

        private void powerImagePointerPressed(object sender, PointerRoutedEventArgs e)
        {
            isOn = !isOn;

            powerImage.Source = new BitmapImage(new Uri(isOn ? "ms-appx:///Assets/PowerOn.png" : "ms-appx:///Assets/PowerOff.png"));
        }
    }
}
